package com.tc.dispatch.web.util;

import java.util.*;
import java.net.*;
import javax.naming.*;
import javax.sql.*;
import javax.ejb.*;
import com.tc.dispatch.gsservice.ejb.*;


/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Company: ZJU</p>
 * @author DxFeng
 * @version 1.0
 */

public class ServiceLocator
{
    private Context ctx;
    private Map cache;       // Cache EJBHomes for re-use
    // -------------------------------------------------------------------------
    // Singleton

    private static ServiceLocator me;

    static
    {
        me = new ServiceLocator();
    }

    private ServiceLocator() throws ServiceLocatorException
    {
        try
        {
            ctx = new InitialContext();
            cache = Collections.synchronizedMap(new HashMap());
        }
        catch (NamingException ne)
        {
            throw new ServiceLocatorException("NamingException: " + ne.getMessage());
        }
    }

    static public ServiceLocator getInstance()
    {
        return me;
    }

    // -------------------------------------------------------------------------------------------------------------------------
    // Service Locator Methods

    public EJBLocalHome getEJBLocalHome(String jndiEJBLocalHomeName) throws ServiceLocatorException
    {
        EJBLocalHome home = null;

        try
        {
            if (cache.containsKey(jndiEJBLocalHomeName))
            {
                home = (EJBLocalHome) cache.get(jndiEJBLocalHomeName);
            }
            else
            {
              Object obj=ctx.lookup(jndiEJBLocalHomeName);

                home = (EJBLocalHome) ctx.lookup(jndiEJBLocalHomeName);
   
                cache.put(jndiEJBLocalHomeName, home);
            }
        }
        catch (NamingException ne)
        {
            throw new ServiceLocatorException("NamingException: " + ne.getMessage());
        }
        return home;
    }

    public DataSource getDataSource(String jndiDataSourceName) throws ServiceLocatorException
    {
        DataSource ds = null;

        try
        {
            if (cache.containsKey(jndiDataSourceName))
            {
                ds = (DataSource) cache.get(jndiDataSourceName);
            }
            else
            {
                ds = (DataSource) ctx.lookup(jndiDataSourceName);
                cache.put(jndiDataSourceName, ds);
            }
        }
        catch (NamingException ne)
        {
            throw new ServiceLocatorException("NamingException: " + ne.getMessage());
        }
        return ds;
    }

    public URL getURL(String jndiURLName) throws ServiceLocatorException
    {
        try
        {
            return (URL) ctx.lookup(jndiURLName);
        }
        catch (NamingException ne)
        {
            throw new ServiceLocatorException("NamingException: " + ne.getMessage());
        }
    }

    public String getString(String jndiStringName) throws ServiceLocatorException
    {
        try
        {
            return (String) ctx.lookup(jndiStringName);
        }
        catch (NamingException ne)
        {
            throw new ServiceLocatorException("NamingException: " + ne.getMessage());
        }
    }

}
